'''
Created on 15.7.2011

@author: gilead
'''
import sys

from direct.task import Task
from direct.showbase.ShowBase import ShowBase
from panda3d.core import Vec4,Vec3 #@UnresolvedImport
from direct.filter.CommonFilters import CommonFilters
from panda3d.core import ColorWriteAttrib #@UnresolvedImport

from panda3d.core import AmbientLight,VBase4 #@UnresolvedImport

from CameraManager import CameraManager
from MouseHandler import MouseHandler
from RDServer import RDServer
from World import World
from SingleLine import SingleLine
from WorldListener import WorldListener
from Arc import ArcUpdater
from ShowArcStatus import ShowArcStatus
from KeyboardHandler import KeyboardHandler
from VisualEffects import VisualEffects

from random import random

class MyApp(ShowBase):
    
    def __init__(self):
        ShowBase.__init__(self)
        self.setBackgroundColor(0,0,0)
        
        # Disable the camera trackball controls.
        self.disableMouse()
        
        self.accept('escape', sys.exit)              #Escape quits
 
        self.world = World("world")
        self.world.reparentTo(render)
        
        self.sfx = VisualEffects(self.world)
        
        self.worldListener = WorldListener(self.world, self.sfx)
        
        self.camManager = CameraManager(self.camera, None)
        
        self.taskMgr.add(self.UpdateLinesToTargetTask, "UpdateLinesToTargetTask")
        self.arcUpdater = ArcUpdater(self.world)
        
        arcstatus = ShowArcStatus(self.world, self.camManager)
        self.keyboard = KeyboardHandler(arcstatus)
        
        # tanne talletetaan viivoja
        self.lines = dict()
        
        #set up lights 
        self.alight = AmbientLight('alight') 
        self.alight.setColor(VBase4(0.7, 0.7, 0.7, 1)) 
        self.alnp = render.attachNewNode(self.alight) 
        render.setLight(self.alnp)
        
        self.filters = CommonFilters(base.win, base.cam)
        filterok = self.filters.setBloom(blend=(0,0,0,1), desat=-0.5, intensity=3.0, size=2)
        
        # estetaan alfan asettaminen.. tama pitaa ylikirjoittaa jos haluaa kayttaa alfaa
        self.render.setAttrib(ColorWriteAttrib.make(ColorWriteAttrib.CRgb))
        
        #self.server = RDServer("red", "secret")
        self.server = RDServer("blue", "secret")
        
        self.mouseHandler = MouseHandler(self.render, self.camera, self.camManager, self.taskMgr, self.server, self.world)
        
        #self.taskMgr.doMethodLater(2, self.laserTestTask, "laserTestTask")
        
    def laserTestTask(self, task):
        x = random() * 10
        y = 0
        self.sfx.fireLaser(101, 0, 102, x, y)
        return Task.again
    
    #def meshDrawerTest(self):
        #self.lasers.add(10, 0.08, Vec3(0,0,0), Vec3(0,100,0), Vec4(100/255,255/255,0,1))
        #self.lasers.add(15, 0.08, Vec3(0,0,0), Vec3(100,0,0), Vec4(127/255,255/255,0,1))
        #self.lasers.add(3, 0.1, Vec3(0,0,0), Vec3(0,0,100), Vec4(1,0,0,1))
        
    def UpdateLinesToTargetTask(self, task):
        for obj in self.world.getChildren():
            ship = obj.getPythonTag('Ship')
            if ship != None:
                line = self.getLineToTargetByShip(ship)
                tgt = ship.movementTarget
                if tgt != None:
                    pos = ship.getPos()
                    min, max = ship.getTightBounds()
                    #med = (max + min) / 2
                    #pos += med
                    #print med, pos
                    line.redrawvectors(tgt, pos)
                else:
                    line.hide()
        return Task.cont
    
    def getLineToTargetByShip(self, ship):
        if not ship.guid in self.lines:
            line = SingleLine(render, 2.0, Vec4(0, 191, 255, 1))
            self.lines[ship.guid] = line
            return line
        return self.lines[ship.guid]
        

app = MyApp()
app.run()